/*
 * This source code is part of TWaver 4.0
 *
 * Serva Software PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * Copyright 2002 - 2011 Serva Software. All rights reserved.
 */

package demo.network.topo.polyline;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.swing.JCheckBox;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;

import twaver.Branch;
import twaver.Element;
import twaver.Generator;
import twaver.PolyLine;
import twaver.Segment;
import twaver.TDataBox;
import twaver.TWaverConst;
import twaver.TerminalPoint;
import twaver.Node;
import twaver.network.TNetwork;
import twaver.network.ui.PolyLineUI;

public class Network3 extends TNetwork{

	private PolyLine polyline3 = new PolyLine();	
	private PolyLine polyline4 = new PolyLine();
	private PolyLine polyline5 = new PolyLine();
	
	private PolyLine blinkingPolyLine = null;
	
	private JCheckBox nodeVisible = new JCheckBox("Node Visible", true);
	private JCheckBox labelVisible = new JCheckBox("Label Visible", false);
	
	public Network3(){
		this.createPolyLine();
		this.interaction();
		
		nodeVisible.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				getCanvas().repaint();
			}
		});
		labelVisible.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				invalidateAllElementBounds(); // this is important
				getCanvas().repaint();
			}
		});
		this.setEnableIllegalLinkVisible(true);
		this.setToolbarByName(TWaverConst.DEFAULT_TOOLBAR);
		this.getToolbar().add(this.nodeVisible);
		this.addVisibleFilter(new twaver.VisibleFilter(){
			public boolean isVisible(Element element) {
				if(element instanceof Node){
					return nodeVisible.isSelected();
				}
				return true;
			}
		});
		this.getToolbar().add(this.labelVisible);
		this.setElementLabelGenerator(new Generator(){
			public Object generate(Object object) {
				if(labelVisible.isSelected()){
					return ((Element)object).getName();
				}
				return null;
			}
		});
	}
	
	public void interaction(){
		final TDataBox box = this.getDataBox();
		this.setSelectableOnRightClick(false);
	
		getCanvas().addMouseListener(new MouseAdapter(){
			public void mouseClicked(MouseEvent e) {
				if(e.getClickCount() == 1 && e.getButton() == MouseEvent.BUTTON3){	
					List selectedSegments = new ArrayList();
					Iterator it = box.iterator();
					while(it.hasNext()){
						Element element = (Element)it.next();
						if(element instanceof PolyLine){
							PolyLineUI ui = (PolyLineUI)getRenderer().getElementUI(element);
							Segment segment = ui.getSegmentPhysicalAt(e.getPoint().x, e.getPoint().y);
							if(segment != null){
								selectedSegments.add(segment);
							}
						}
					}
					JPopupMenu popupMenu = new JPopupMenu();
					if(selectedSegments.size() == 0 && blinkingPolyLine != null){
						JMenuItem item = new JMenuItem("Stop Blink");
						popupMenu.add(item);
						item.addActionListener(new ActionListener(){
							public void actionPerformed(ActionEvent e) {
								if(blinkingPolyLine != null){
									blinkingPolyLine.setBlinkingObject(null);
								}
								blinkingPolyLine = null;
							}
						});
					}else{
						for(int i=0; i<selectedSegments.size(); i++){
							final Segment segment = (Segment)selectedSegments.get(i);
							final Branch branch = segment.getBranch();
							final PolyLine polyline = segment.getPolyLine();
							
							JMenu menu = new JMenu(polyline.getName());
							popupMenu.add(menu);
							
							JMenuItem item = new JMenuItem("Select Segment");
							menu.add(item);
							item.addActionListener(new ActionListener(){
								public void actionPerformed(ActionEvent e) {
									if(blinkingPolyLine != null){
										blinkingPolyLine.setBlinkingObject(null);
									}
									blinkingPolyLine = polyline;
									blinkingPolyLine.setBlinkingObject(segment);
									
								}
							});
							
							if(branch != null){
								item = new JMenuItem("Select Branch");
								menu.add(item);
								item.addActionListener(new ActionListener(){
									public void actionPerformed(ActionEvent e) {
										if(blinkingPolyLine != null){
											blinkingPolyLine.setBlinkingObject(null);
										}
										blinkingPolyLine = polyline;
										blinkingPolyLine.setBlinkingObject(branch);
									}
								});
							}
							
							item = new JMenuItem("Select Polylne");
							menu.add(item);
							item.addActionListener(new ActionListener(){
								public void actionPerformed(ActionEvent e) {
									if(blinkingPolyLine != null){
										blinkingPolyLine.setBlinkingObject(null);
									}
									blinkingPolyLine = polyline;
									blinkingPolyLine.setBlinkingObject(polyline);
								}
							});
							
						}
					}
					if(popupMenu.getComponentCount() > 0){
						popupMenu.show(getCanvas(), e.getPoint().x, e.getPoint().y);
					}
				}
			}
		});
	}

	public void createPolyLine(){
		createPolyLine(polyline3, 50,40);
		createPolyLine(polyline4, 250, 40);
		createPolyLine(polyline5, 400, 40);
		
		polyline3.setName("polyline3");
		polyline4.setName("polyline4");
		polyline5.setName("polyline5");
		
	}
	
	private void createPolyLine(PolyLine polyline, int ox, int oy){
		TDataBox box = this.getDataBox();
		
        polyline.putLinkOutlineColor(Color.GREEN);
        polyline.putLinkWidth(3);
        polyline.putLinkOutlineWidth(0);
		 
		Node node1 = new TerminalPoint(); node1.setName("1"); node1.setLocation(ox + 0, oy + 0); box.addElement(node1);		
		Node node2 = new TerminalPoint(); node2.setName("2"); node2.setLocation(ox + 0, oy + 100); box.addElement(node2);
		Node node3 = new TerminalPoint(); node3.setName("3"); node3.setLocation(ox + 50, oy + 50); box.addElement(node3);
		Node node4 = new TerminalPoint(); node4.setName("4"); node4.setLocation(ox + 100, oy + 50); box.addElement(node4);		
		Node node5 = new TerminalPoint(); node5.setName("5"); node5.setLocation(ox + 150, oy + 50); box.addElement(node5);
		Node node6 = new TerminalPoint(); node6.setName("6"); node6.setLocation(ox + 200, oy + 0); box.addElement(node6);
		Node node7 = new TerminalPoint(); node7.setName("7"); node7.setLocation(ox + 200, oy + 100); box.addElement(node7);		
		Node node8 = new TerminalPoint(); node8.setName("8"); node8.setLocation(ox + 100, oy + 100); box.addElement(node8);
		Node node9 = new TerminalPoint(); node9.setName("9"); node9.setLocation(ox + 50, oy + 150); box.addElement(node9);
		Node node10 = new TerminalPoint(); node10.setName("10"); node10.setLocation(ox + 150, oy + 150); box.addElement(node10);
		
		Branch branch = new Branch();
		branch.setBranchColor(Color.YELLOW);
		polyline.addBranch(branch);
		branch.addSegment(createSegment(node1, node3));
		branch.addSegment(createSegment(node2, node3));
		branch.addSegment(createSegment(node4, node3));
		
		branch = new Branch();
		branch.setBranchColor(Color.BLUE);
		polyline.addBranch(branch);
		branch.addSegment(createSegment(node8, node4));
		branch.addSegment(createSegment(node8, node9));
		branch.addSegment(createSegment(node8, node10));
		
		branch = new Branch();
		branch.setBranchColor(Color.BLACK);
		polyline.addBranch(branch);
		branch.addSegment(createSegment(node5, node4));
		branch.addSegment(createSegment(node5, node6));
		branch.addSegment(createSegment(node5, node7));
		
		box.addElement(polyline);
		
	}

	private Segment createSegment(Node from, Node to){
		Segment segment = new Segment();
		segment.setFromNode(from);
		segment.setToNode(to);
		return segment;
	}
	
}